package org.niux.aims.dao.impl;

import java.util.List;
import java.util.logging.Level;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.niux.aims.dao.IMenuDAO;
import org.niux.aims.models.EntityManagerHelper;
import org.niux.aims.models.Menu;
import org.springframework.stereotype.Repository;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：MenuDAO.java<br>
 * 摘    要：菜单数据操作类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2013-7-30<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
@Repository(value="menuDao")
public class MenuDAO implements IMenuDAO {
	// property constants
	public static final String MENU_NAME = "menuName";
	public static final String MENU_CODE = "menuCode";
	public static final String LINK = "link";
	public static final String LEVEL = "level";

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	/**
	 * Perform an initial save of a previously unsaved Menu entity. All
	 * subsequent persist actions of this entity should use the #update()
	 * method. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#persist(Object)
	 * EntityManager#persist} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * MenuDAO.save(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            Menu entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Menu entity) {
		EntityManagerHelper.log("saving Menu instance", Level.INFO, null);
		try {
			getEntityManager().persist(entity);
			EntityManagerHelper.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent Menu entity. This operation must be performed within
	 * the a database transaction context for the entity's data to be
	 * permanently deleted from the persistence store, i.e., database. This
	 * method uses the {@link javax.persistence.EntityManager#remove(Object)
	 * EntityManager#delete} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * MenuDAO.delete(entity);
	 * EntityManagerHelper.commit();
	 * entity = null;
	 * </pre>
	 * 
	 * @param entity
	 *            Menu entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Menu entity) {
		EntityManagerHelper.log("deleting Menu instance", Level.INFO, null);
		try {
			entity = getEntityManager()
					.getReference(Menu.class, entity.getId());
			getEntityManager().remove(entity);
			EntityManagerHelper.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved Menu entity and return it or a copy of it to
	 * the sender. A copy of the Menu entity parameter is returned when the JPA
	 * persistence mechanism has not previously been tracking the updated
	 * entity. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * entity = MenuDAO.update(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            Menu entity to update
	 * @return Menu the persisted Menu entity instance, may not be the same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public Menu update(Menu entity) {
		EntityManagerHelper.log("updating Menu instance", Level.INFO, null);
		try {
			Menu result = getEntityManager().merge(entity);
			EntityManagerHelper.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public Menu findById(Integer id) {
		EntityManagerHelper.log("finding Menu instance with id: " + id,
				Level.INFO, null);
		try {
			Menu instance = getEntityManager().find(Menu.class, id);
			return instance;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}
	
	/**
	 * 
	 * 方法概述：获取所有菜单
	 * 描述：获取所有菜单
	 * @return
	 * 返回类型：List<Menu>
	 * 修改人：梁铭显
	 * 修改时间：2013-7-30
	 */
	@SuppressWarnings("unchecked")
	public List<Menu> findAllMenus(){
		StringBuffer hql = new StringBuffer();
		
		hql.append("SELECT DISTINCT menu ");
		hql.append("FROM Menu menu ");
		hql.append("LEFT JOIN FETCH menu.menus menus ");
		hql.append("WHERE 1=1 ");
		hql.append("AND menu.level = 1");
		
		Query query = getEntityManager().createQuery(hql.toString());
		
		List<Menu> menus = query.getResultList();
		return menus;
	}
	
	/**
	 * 
	 * 方法概述：根据编码获取菜单
	 * 描述：根据编码获取菜单
	 * @return
	 * 返回类型：List<Menu>
	 * 修改人：梁铭显
	 * 修改时间：2013-9-27
	 */
	@SuppressWarnings("unchecked")
	public List<Menu> findMenusByCode(String codes){
		StringBuffer hql = new StringBuffer();
		
		hql.append("SELECT DISTINCT menu ");
		hql.append("FROM Menu menu ");
		hql.append("LEFT JOIN FETCH menu.menus menus ");
		hql.append("WHERE 1=1 ");
		hql.append("AND menu.level = 1 ");
		hql.append("AND menu.menuCode IN ("+codes+") ");
		
		Query query = getEntityManager().createQuery(hql.toString());
		
		List<Menu> menus = query.getResultList();
		
		return menus;
	}
}